package com.haixue.android.academy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.haixue.android.academy.utils.MyParams;

import org.apache.log4j.Logger;

import butterknife.ButterKnife;
import cn.woblog.android.common.skin.core.ThemeManager;
import cn.woblog.android.common.utils.SharePreferenceUtils;

/**
 * Created by ren on 2015/5/4 0004.
 */
public abstract class BaseFragment extends Fragment{
    protected final static Logger log = Logger.getLogger(new Exception()
            .getStackTrace()[2].getClassName());
    protected MyParams myParams;

    protected  final SharePreferenceUtils sp = SharePreferenceUtils.getInstance(getActivity());
    /**
     * 找控件
     */
    protected void initViews() {

    }

    /**
     * 动态设置样式，颜色，宽高，背景
     */
    protected void initStyles() {
    }

    /**
     *设置数据
     */
    protected void initDatas() {
        myParams = new MyParams(getActivity(), sp);
    }

    /**
     * 绑定监听器
     */
    protected void initListener() {
    }


    /**通过主题管理器获取对应的颜色
     * @param id
     * @return
     */
    protected int getColor(int id){
        return ThemeManager.getColor(id);
    }

    /**通过主题管理器获取对应的尺寸
     * @param id
     * @return
     */
    protected int getDimen(int id){
        return ThemeManager.getDimen(id);
    }

    /**通过主题管理器获取对应的drawable
     * @param id
     * @return
     */
    protected android.graphics.drawable.Drawable getDrawable(int id){
        return ThemeManager.getDrawable(id);
    }

    @Override
    public void onResume() {
        if (ThemeManager.isRefreshUI()) {
            initStyles();
        }
        super.onResume();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = getLayoutView(inflater,container,savedInstanceState);
        ButterKnife.inject(this, view);
        initViews();
        initStyles();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initDatas();
        initListener();
        super.onViewCreated(view, savedInstanceState);
    }

    /**返回要显示的View
     * @return
     * @param inflater
     * @param container
     * @param savedInstanceState
     */
    protected abstract View getLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.reset(this);
    }
}
